//渲染国内折扣商品详情页面
function bindHtml(arr) {
    let html = ""
    arr.forEach(item => {
        html += ` 
        <ul id="info">
            <li>
                <div class="infoR">
                    <div class="title">
                        <h4>${item.productName}</h4>
                    </div>
                    <div class="other">
                        <span>${item.productFrom} | ${item.productTime}</span>
                        <span>${item.productTips}</span>
                        <div>${item.productPrice}</div>
                    </div>
                    <div class="container">
                        <p>${item.productInfo}</p>
                        <div class="img">${item.productImg}</div>
                    </div>
                </div>
            </li>
        </ul>`
    })
    //一定要return 否则页面内容undefined
    return html;
}

//获取商品的productid，请求对应的详情页面(方法一)有局限
/* var str = window.location.href
str = str.substring(56, 57);
ajax({
    url: 'http://chst.vip:1234/api/getdiscountproduct?productid=' + str
}).then(res => {
    let html = bindHtml(res.result)
    product.innerHTML = html
}) */


//获取浏览器的query参数
window.onload = function () {//放到onload里面不会暴露太多变量
    function getUrlParams() {
        let href = location.href;
        let regExp = /.+\?(.+)/
        let param = regExp.exec(href)[1]
        // console.log(param);
        // console.log(decodeURI(param));解码URI的参数编码
        let paramArr = param.split("&");
        //console.log(paramArr);
        let query = {}
        paramArr.forEach(item => {
            var itemArr = item.split("=");
            // console.log(itemArr);
            query[itemArr[0]] = decodeURI(itemArr[1])
        })
        return query
    }
    let query = getUrlParams()
    //获取浏览器query参数 {productid: "0", title: "国内折扣"}
    console.log(query);

    ajax({
        url: 'http://chst.vip:1234/api/getdiscountproduct',
        data: query
    }).then(res=>{
        let html = bindHtml(res.result)
        product.innerHTML = html
    })
}




